package com._51doit.day02;

import java.util.ArrayList;

/**
 * @create: 2021-10-08 21:40
 * @author: 今晚打脑斧
 * @program: MyList
 * @Description:
 **/
public class MyList<T>  extends ArrayList<T> {
    //map
    public <R> MyList<R> map(Function1<T,R> func) {
        MyList<R> list = new MyList<>() ;
        for (T t : this) {
            R invote = func.invote(t);
            list.add(invote);
        }
        return list ;
    }
    //filter
    public MyList<T> filter(Function1<T,Boolean> func) {
        MyList<T> list = new MyList<>() ;
        for (T t : this) {
            if (func.invote(t)){
                list.add(t);
            }
        }
        return list ;
    }
    //reduce
    public T reduce (Function2<T,T,T> func){
        T temp = null ;
        boolean flag = true ;
        for (T t : this) {
            if (flag){
                temp=t ;
                flag=false ;
            }else{
                temp=func.invote(temp,t);
            }
        }
        return temp ;
    }
}
